<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>用户指南 - 编译程序指令 (针对不同操作系统的不同行为)</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="5">用户指南 - 编译程序指令 (针对不同操作系统的不同行为)</font></b></p>

这将是我们最后一次访问文件属性程序。还有一个先前讨论克服的局限性，我们已经将它直到现在，因为它是一种特殊情况。<br><br>So far the Attributes column on the display has simply been an <a href="variables.html">integer</a>. This is because the return values of the <a href="../filesystem/getfileattributes.html">GetFileAttributes()</a> and <a href="../filesystem/directoryentryattributes.html">DirectoryEntryAttributes()</a> instructions have a different meaning on Windows systems compared with Mac and Linux systems. <br><br>We can't allow for this difference at run-time, however we can use <a href="compilerdirectives.html">Compiler Directives</a> to have the program behave differently on the three different operating systems. <br><br>Add this new procedure declaration to that section. 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">Declare</font></b>.s <font color="#3A3966">AttributeString</font>(Attributes.l)
</font></pre>



Add this new procedure to the implementation section. 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">Procedure</font></b>.s <font color="#3A3966">AttributeString</font>(Attributes.l)
    <font color="#3A3966">; Converts an integer attributes value into a string description.</font>
    <font color="#3A3966">; Supports Linux, Mac and Windows system's attributes.</font>
    
    <b><font color="#3A3966">Protected</font></b>.s Result
    
    Result = ""
    
    <b><font color="#3A3966">CompilerIf</font></b> <font color="#924B72">#PB_Compiler_OS</font> = <font color="#924B72">#PB_OS_Windows</font>
      ??
      ??<font color="#3A3966">; These are the attributes for Windows systems.</font>
      ??<font color="#3A3966">; A logical-and of the attribute with each constant filters out that bit and can then be used for comparison.</font>
      
      ??<b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Archive</font>
        ???Result + "A"
      ??<b><font color="#3A3966">Else</font></b>
        ???Result + " "
      ??<b><font color="#3A3966">EndIf</font></b>
      ??
      ??<b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Compressed</font>
        ???Result + "C"
      ??<b><font color="#3A3966">Else</font></b>
        ???Result + " "
      ??<b><font color="#3A3966">EndIf</font></b>
      ??
      ??<b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Hidden</font>
        ???Result + "H"
      ??<b><font color="#3A3966">Else</font></b>
        ???Result + " "
      ??<b><font color="#3A3966">EndIf</font></b>
      ??
      ??<b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadOnly</font>
        ???Result + "R"
      ??<b><font color="#3A3966">Else</font></b>
        ???Result + " "
      ??<b><font color="#3A3966">EndIf</font></b>
      ??
      ??<b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_System</font>
        ???Result + "S"
      ??<b><font color="#3A3966">Else</font></b>
        ???Result + " "
      ??<b><font color="#3A3966">EndIf</font></b>
      ??
    <b><font color="#3A3966">CompilerElse</font></b>
      
      <font color="#3A3966">; These are the attributes for Mac and Linux systems.</font>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_Link</font>
        Result + "L "
      <b><font color="#3A3966">Else</font></b>
        Result + "?"
      <b><font color="#3A3966">EndIf</font></b>
      
      <font color="#3A3966">; User attributes.</font>
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadUser</font>
        Result + "R"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_WriteUser</font>
        Result + "W"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ExecUser</font>
        Result + "X "
      <b><font color="#3A3966">Else</font></b>
        Result + "?"
      <b><font color="#3A3966">EndIf</font></b>
      
      <font color="#3A3966">; Group attributes.</font>
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadGroup</font>
        Result + "R"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_WriteGroup</font>
        Result + "W"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ExecGroup</font>
        Result + "X "
      <b><font color="#3A3966">Else</font></b>
        Result + "?"
      <b><font color="#3A3966">EndIf</font></b>
      
      <font color="#3A3966">; All attributes.</font>
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ReadAll</font>
        Result + "R"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_WriteAll</font>
        Result + "W"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
      <b><font color="#3A3966">If</font></b> Attributes &amp; <font color="#924B72">#PB_FileSystem_ExecAll</font>
        Result + "X"
      <b><font color="#3A3966">Else</font></b>
        Result + " "
      <b><font color="#3A3966">EndIf</font></b>
      
    <b><font color="#3A3966">CompilerEndIf</font></b>
    
    <font color="#3A3966">; Return the result.</font>
    <b><font color="#3A3966">ProcedureReturn</font></b> Result
    
  <b><font color="#3A3966">EndProcedure</font></b>
</font></pre>



Finally, replace these two lines in the ListLoad procedure 
 
<pre><font face="Courier New, Courier, mono"size="2">      <font color="#3A3966">; Convert the attributes to a string, for now.</font>
      Attrib =<font color="#3A3966"> StrU</font>(<font color="#3A3966">Files</font>()\Attributes)
</font></pre>



with these, 
 
<pre><font face="Courier New, Courier, mono"size="2">      <font color="#3A3966">; Call AttributeString to convert the attributes to a string representation.</font>
      Attrib =<font color="#3A3966"> AttributeString</font>(<font color="#3A3966">Files</font>()\Attributes)
</font></pre>



Now when the program is executed a string display will be shown instead of the integer values. 
On a Windows system it would look something like this (assuming all attributes are set): 
 
<pre><font face="Courier New, Courier, mono"size="2">  ACHRS
</font></pre>



and on the other two systems: 
 
<pre><font face="Courier New, Courier, mono"size="2">  L RWX RWX RWX
</font></pre>



The <a href="compilerdirectives.html">CompilerIf</a> instruction works much like an 
<a href="if_endif.html">If</a> instruction - however it is the compiler that makes the 
decision at compile-time, rather than the executable at run-time. This means that we can 
include different code to handle the file attributes on the different operating systems. 
<br>
<br>
The lines between: 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">CompilerIf</font></b> <font color="#924B72">#PB_Compiler_OS</font> = <font color="#924B72">#PB_OS_Windows</font>
</font></pre>



and 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">CompilerElse</font></b>
</font></pre>



will be compiled on Windows systems. The constant <font color="#924B72">#PB_Compiler_OS</font> is automatically defined 
by PureBasic to allow this kind of program logic. 
<br>
<br>
The other section will be used on Mac and Linux systems - which work the same way, 
conveniently. If these operating systems had different attribute values too, then we could 
use <a href="compilerdirectives.html">CompilerSelect</a> and <a href="compilerdirectives.html">CompilerCase</a> 
to make a three-way determination. 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">CompilerSelect</font></b> <font color="#924B72">#PB_Compiler_OS</font>
      
    <b><font color="#3A3966">CompilerCase</font></b> <font color="#924B72">#PB_OS_Linux</font>
      <font color="#3A3966">; Code for Linux systems.</font>
      

    <b><font color="#3A3966">CompilerCase</font></b> <font color="#924B72">#PB_OS_MacOS</font>
      <font color="#3A3966">; Code for Mac systems.</font>
      
    <b><font color="#3A3966">CompilerCase</font></b> <font color="#924B72">#PB_OS_Windows</font>
      <font color="#3A3966">; Code for Windows systems.</font>
      
  <b><font color="#3A3966">CompilerEndSelect</font></b>
</font></pre>



The last compiler directive that we're going to discuss here is: 
<a href="compilerdirectives.html">EnableExplicit</a>. 
<br>
<br>
There is a good reason for using this directive. It requires that all <a href="variables.html">variables</a> 
must be defined explicitly before usage, in some way, (using <a href="define.html">Define</a>, 
<a href="dim.html">Dim</a>, <a href="global.html">Global</a>, <a href="protected.html">Protected</a>, 
<a href="static.html">Static</a> etc.) Doing so eliminates the possibility of logic errors due 
to mistyped variable names being defined "on-the-fly". This type of subtle error will not affect 
a program's compilation but could well present as an inconvenient bug at run-time. Using this 
directive eliminates this kind of problem as a compiler error would occur. 
<br>
<br>
For example: 
 
<pre><font face="Courier New, Courier, mono"size="2">  <b><font color="#3A3966">EnableExplicit</font></b>
  
  <b><font color="#3A3966">Define</font></b>.l Field, FieldMax
  
  <font color="#3A3966">; ...</font>
  
  <b><font color="#3A3966">If</font></b> Field &lt; FieldMax
    <font color="#3A3966">; If EnableExplicit is omitted this section of code may not execute when intended because FieldMax will be zero.</font>
  <b><font color="#3A3966">EndIf</font></b>
</font></pre>


<p><b>用户指南导航</b></p><blockquote>

<a href="ug_procedures.html">&lt; 上一篇: 过程中使用结构化代码</a> | <a href="ug_overview.html">概述</a> | <a href="ug_files.html">下一篇: 文件的读取与写入 &gt;</a> 
</body></html>